/**
 * Created by vicket on 2017/4/14.
 */
$(function(){

    //返回
    var back=$(".back");
    var bookId=sessionStorage.getItem("bookId");
    back.attr("href","noveldetail.ejs#"+bookId);


    var novelContent=$(".novel_content");
    var fixedModel=$(".fixed_model");
    var footer=$(".footer");
    var setTypeface=$(".set_typeface");
    var  typeface=$(".typeface");
    var isShow=true;
    novelContent.on("touchstart",function(){
        if(isShow){
            isShow=false;
            fixedModel.show();
            setTypeface.hide();
            footer.show();
        }else{
            isShow=true;
            fixedModel.hide();
            setTypeface.hide();
            footer.show();
        }
    });
    typeface.on("touchstart",function(){
        setTypeface.show();
        footer.hide();
    });

    //设置字体
    var typefaceAdd=$(".typeface_add");
    var typefaceSubstract=$(".typeface_substract");
    var fontSize=0;
    typefaceAdd.on("touchstart",function(){
            fontSize=parseInt(novelContent.css("font-size"));
            fontSize++;
            if(fontSize>26){
                fontSize=26;
            }
            novelContent.css("font-size",fontSize+"px");
        }
    )
    typefaceSubstract.on("touchstart",function(){
            fontSize=parseInt(novelContent.css("font-size"));
            fontSize--;
            if(fontSize<=12){
                fontSize=12;
            }
            novelContent.css("font-size",fontSize+"px");
        }
    );
    //设置颜色背景
    var spans=$(".read_background span");
    spans.on("touchstart",function(){
        var bg=$(this).css("background");
        $("body").css("background",bg);
    });
    //设置护眼
    var eyeshield=$(".eyeshield");
    var darkIcont=$(".eyeshield span");
    var dayText=$(".day_text");
    var isDark=true;
    eyeshield.on("touchstart",function(){
        if(isDark){
            isDark=false;
            darkIcont.addClass("dark_icont");
            dayText.text("晚上");
        }else{
            isDark=true;
            darkIcont.removeClass("dark_icont");
            dayText.text("白天");
        }
    });


    //获取章节内容
   var novelcontent=$(".novel_content");
    var anchor=window.location.hash.slice(1);
    console.log(location.search);
    $.ajax({
        type:"get",
        url:"/api/frontGetNovel",
        data:{chapter:anchor},
        success:function(result){
            novelcontent.html(result.data);
            var timmer=$(".info");
            var admire=$(".admire-wrap");
            timmer.css("display",'none');
            admire.css("display",'none');
           console.log(result);


            //翻页
            var letterw=anchor.match(/[a-z]+/)[0];
            var lettern=anchor.match(/\d+/)[0];
            var prev=$(".prev");
            var next=$(".next");
            var totalsession=parseFloat(sessionStorage.getItem("totalSection"));
            //下一页
            next.click(function(){
                lettern++;
                if(lettern>=totalsession){
                    lettern=totalsession
                }else{
                    $.ajax({
                        type:"get",
                        url:"/api/frontGetNovel",
                        data:{chapter:letterw+lettern},
                        success:function(result){
                            novelcontent.html(result.data);
                            var timmer=$(".info");
                            var admire=$(".admire-wrap");
                            timmer.css("display",'none');
                            admire.css("display",'none');
                        }
                    })
                }
                console.log(lettern);

            });
            //上一页
            prev.click(function(){
                lettern--;
                if(lettern<=0){
                    lettern=0;
                }else{
                    console.log(lettern);
                    $.ajax({
                        type:"get",
                        url:"/api/frontGetNovel",
                        data:{chapter:letterw+lettern},
                        success:function(result){
                            novelcontent.html(result.data);
                            var timmer=$(".info");
                            var admire=$(".admire-wrap");
                            timmer.css("display",'none');
                            admire.css("display",'none');

                        }
                    })
                }

                console.log(lettern);


            })


        }
    });


})